#coding:utf8
# vim: tabstop=4 shiftwidth=4 softtabstop=4
"""
Created on May 9, 2013

@author: join
"""
import os
import datetime
from cvirt.common.config import load_settings
from cvirt.manager.monitor.plugins.cpu import CPU
from cvirt.manager.monitor.plugins.disk import Disk
from cvirt.manager.monitor.plugins.interface import Interface
from cvirt.manager.monitor.plugins.load import Load
from cvirt.manager.monitor.plugins.memory import Memory
from cvirt.manager.monitor.plugins.vcpu import VCPU
from cvirt.manager.monitor.plugins.vdisk import VDisk
from cvirt.manager.monitor.plugins.vinterface import VInterface


class Grapher(object):
    """graph pictures use specified hostname and time range"""
    def __init__(self,hostname,metric=None,starttime=None,endtime=None,timerange=None, type=None):
        self.hostname = hostname
        self.timerange = timerange
        self.starttime = starttime
        self.endtime = endtime
        self.metric = metric
        self.type = type
        self.init()

    def init(self):
        conf = load_settings()
        self.data_dir = conf.get('collectd_data_path','/var/lib/collectd/rrd/')
        self.data_dir = os.path.join(self.data_dir,self.hostname)
        self.graph_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)),'static/images/monitor')

    def gen_time_range(self,starttime,endtime):
        init_time = datetime.datetime(1970,1,1,0,0,0)
        epoch_start = datetime.datetime.strptime(starttime,"%Y-%m-%d %H:%M:%S")
        epoch_end = datetime.datetime.strptime(endtime,"%Y-%m-%d %H:%M:%S")

        starttime = int((epoch_start-init_time).total_seconds())-28800
        endtime = int((epoch_end-init_time).total_seconds())-28800
        return "%s:%s"%(starttime,endtime)

    def graphvm(self):
        result= {"cpu":[],"disk":[],"interface":[]}
        #设置监控时间段
        if self.starttime and self.endtime:
            self.timerange = self.gen_time_range(self.starttime, self.endtime)
        elif not self.timerange:
            self.timerange = "day" #没有时间段，则设置为day

        vcpu = VCPU(self.data_dir,self.graph_dir)
        vcpu.set_time(self.timerange)
        cpu_graphs = vcpu.gen()
        vcpu_values = []
        for graph_url in cpu_graphs:
            vcpu_values.append(graph_url)
        result["cpu"] = vcpu_values

        vdisk = VDisk(self.data_dir,self.graph_dir)
        vdisk.set_time(self.timerange)
        disk_graphs = vdisk.gen()
        disk_values = []
        for disk_url in disk_graphs:
            disk_values.append(disk_url)
        result["disk"] = disk_values

        interface = VInterface(self.data_dir,self.graph_dir)
        interface.set_time(self.timerange)
        interface_graphs = interface.gen()
        interface_values = []
        for interface_url in interface_graphs:
            interface_values.append(interface_url)
        result["interface"] = interface_values

        return result

    def graph(self):
        if self.type == 'instance':
            return  self.graphvm()

        result= {"cpu":[],"memory":[],"load":[],"disk":[],"interface":[]}
        #graph CPU monitor image
        #设置监控时间段
        if self.starttime and self.endtime:
            self.timerange = self.gen_time_range(self.starttime, self.endtime)
        elif not self.timerange:
            self.timerange = "day" #没有时间段，则设置为day
            
        cpu = CPU(self.data_dir,self.graph_dir)
        cpu.set_time(self.timerange)
        cpu_graphs = cpu.gen()
        cpu_values = []
        for graph_url in cpu_graphs:
            cpu_values.append(graph_url)
        result["cpu"] = cpu_values
        
        # df = Df(self.data_dir,self.graph_dir)
        # df.set_time(self.timerange)
        # df_graphs = df.gen()
        # df_values = []
        # for graph_url in df_graphs:
        #     df_values.append(graph_url)
        # result["df"] = df_values
        
        disk = Disk(self.data_dir,self.graph_dir)
        disk.set_time(self.timerange)
        disk_graphs = disk.gen()
        disk_values = []
        for disk_url in disk_graphs:
            disk_values.append(disk_url)
        result["disk"] = disk_values

        interface = Interface(self.data_dir,self.graph_dir)
        interface.set_time(self.timerange)
        interface_graphs = interface.gen()
        interface_values = []
        for interface_url in interface_graphs:
            interface_values.append(interface_url)
        result["interface"] = interface_values

        load = Load(self.data_dir,self.graph_dir)
        load.set_time(self.timerange)
        load_graphs = load.gen()
        load_values = []
        for load_url in load_graphs:
            load_values.append(load_url)
        result["loads"] = load_values

        memory = Memory(self.data_dir,self.graph_dir)
        memory.set_time(self.timerange)
        memory_graphs = memory.gen()
        memory_values = []
        for memory_url in memory_graphs:
            memory_values.append(memory_url)
        result['memory'] = memory_values
        return result
    